//
//  GMScrollBar.m
//  GomeEShop
//
//  Created by tangli on 15/3/23.
//  Copyright (c) 2015年 Gome. All rights reserved.
//

#import "GMScrollBar.h"

@interface GMScrollBar()
<
UIScrollViewDelegate
>

@property (nonatomic, weak) GMCategaryBar *categaryBar; // 分类选项卡

@property (nonatomic, weak) UIScrollView *scrv;

@property (nonatomic, assign) CGFloat fltControllerW; // 每个控制器的宽度

@property (nonatomic, assign) CGFloat fltControllerH; // 每个控制器的高度

@property (nonatomic, assign) BOOL isShowCategaryBar; // 是否显示categaryBar的一个开关

@property (nonatomic, assign) BOOL isCanScroll; // 是否支持scrollView的滚动


@end

@implementation GMScrollBar

- (GMCategaryBar *)categaryBar
{
    if (_categaryBar == nil)
    {
        
        GMCategaryBar *categaryBar = [GMCategaryBar categaryBarWithArrayTitle:self.aryMTitle
                                                           dividerIsEqualBtnW:NO];
        categaryBar.frame = CGRectMake(0, 0, screenW, CATEGARYBAR_H);
        categaryBar.delegate = self;
        _categaryBar = categaryBar;
        [self addSubview:categaryBar];
    }
    return _categaryBar;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _isShowCategaryBar = YES;
        
        // 创建scrollView
        UIScrollView *scrollView = [[UIScrollView alloc]init];
        CGFloat scrollViewY = _isShowCategaryBar?CATEGARYBAR_H:0;
        scrollView.frame = CGRectMake(0,
                                      scrollViewY,
                                      screenW,
                                      screenH - scrollViewY);
        
        _fltControllerW = scrollView.frame.size.width;
        _fltControllerH = scrollView.frame.size.height;
        
        scrollView.delegate = self; // 设置scrollView的代理
        
        // 隐藏水平方向的滚动条
        scrollView.showsHorizontalScrollIndicator = NO;
        // 隐藏竖直方向的滚动条
        scrollView.showsVerticalScrollIndicator = NO;
        
        // 取消弹簧效果
        scrollView.bounces =NO;
        
        //增加上中左右额外的区域
        scrollView.contentInset =UIEdgeInsetsMake(0, 0, 0, 0);
        
        scrollView.pagingEnabled = YES;
        
        _scrv = scrollView;
        
        [self addSubview:scrollView];
    }
    return self;
}

+ (instancetype)scrollBar
{
    return [[self alloc]initWithFrame:CGRectMake(0,
                                                GMK_NavBar_H,
                                                screenW,
                                                screenH - GMK_NavBar_H)];
}

- (void)setAryMVC:(NSMutableArray *)aryMVC
{
    _aryMVC = aryMVC;
    
    if (aryMVC.count == 0 || aryMVC.count == 1)
    {
        _isShowCategaryBar = NO;
        CGFloat scrollViewY = _isShowCategaryBar?CATEGARYBAR_H:0;
        _scrv.frame = CGRectMake(0,
                                 scrollViewY,
                                 screenW,
                                 screenH - scrollViewY);
    }
    else
    {
        _isShowCategaryBar = YES;
    }
    
    UIViewController *controller = _aryMVC[0];
    controller.view.frame = CGRectMake(0,0,_fltControllerW,_fltControllerH);
    
    [_scrv addSubview:controller.view];
    
    // 设置categaryBar的内容尺寸
    _scrv.contentSize =  CGSizeMake(_fltControllerW * _aryMVC.count, 0);
    
}

-(void)setAryMTitle:(NSMutableArray *)aryMTitle
{
    _aryMTitle = aryMTitle;
    
    if (aryMTitle.count == 0 || aryMTitle.count == 1)
    {
        _isShowCategaryBar = NO;
        
        CGFloat scrollViewY = _isShowCategaryBar?CATEGARYBAR_H:0;
        _scrv.frame = CGRectMake(0,
                                 scrollViewY,
                                 screenW,
                                 screenH - scrollViewY);
    }
    else
    {
        _isShowCategaryBar = YES;
    }
    [self categaryBar];
}

// 是否显示categaryBar的一个开关
- (void)categaryBarShow:(BOOL)isShowCategaryBar
{
    _isShowCategaryBar = isShowCategaryBar;
    
    if (_isShowCategaryBar == NO)
    {
        CGFloat scrollViewY = _isShowCategaryBar?CATEGARYBAR_H:0;
        _scrv.frame = CGRectMake(0,
                                 scrollViewY,
                                 screenW,
                                 screenH - scrollViewY);
    }
}

// 是否支持scrollView的滚动
- (void)scrollBarScroll:(BOOL)isCanScroll
{
    _isCanScroll = isCanScroll;
    
    if (_isCanScroll == NO)
    {
        _scrv.scrollEnabled = NO;
    }
}


#pragma mark - 代理GMCategaryBarDelegate

- (void)categaryBarOptionalDelegate:(GMCategaryBar *)categaryBar withBtnIndex:(NSInteger)index
{
    CGFloat offsetX = index * screenW;
    
    // 创建我的关注控制器
    UIViewController *controller = _aryMVC[index];
    
    if (![_scrv.subviews containsObject:controller.view])
    {
        controller.view.frame = CGRectMake(_fltControllerW * index,
                                           0,
                                           _fltControllerW,
                                           _fltControllerH);
        [_scrv addSubview:controller.view];
    }
 
    // 自动滑动categaryBar的相应的距离
    [_scrv setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

#pragma mark - 代理UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:_scrv])
    {
        NSInteger index = (int)((float)scrollView.contentOffset.x / scrollView.frame.size.width);
        
        [_categaryBar automaticScrollDivider:index];
        
        // 创建我的关注控制器
        UIViewController *controller = _aryMVC[index];
        
        if (![_scrv.subviews containsObject:controller.view])
        {
            controller.view.frame = CGRectMake(_fltControllerW * index,
                                               0,
                                               _fltControllerW,
                                               _fltControllerH);
            [_scrv addSubview:controller.view];
        }
    }
}

@end
